<?php

	// TITLE: Material AUTO MODEL
	// FILE: scaffolding/model/material.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createMaterial = function($con, $material) {
		$sql = 'INSERT INTO material (donation_id, value, description) VALUES (';
		$sql .= $material->getDonationId() . ', ';
		$sql .= $material->getValue() . ', ';
		$sql .= $material->getDescription() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readMaterial = function($con, $id) {
		$sql = 'SELECT * FROM material WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$material = new Material();
			$material->setDonationId($dbio->select($readDonation(result[0])));
			$material->setValue(result[1]);
			$material->setDescription(result[2]);
		} else {
			$material = false;
		}
		return $material;
	};

	$updateMaterial = function($con, $material) {
		$sql = 'UPDATE material SET ';
		$sql .= 'donation_id = ' . $material->getDonationId() . ', ';
		$sql .= 'value = ' . $material->getValue() . ', ';
		$sql .= 'description = ' . $material->getDescription() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deleteMaterial = function($con, $id) {
		$sql = 'DELETE FROM material WHERE donation_id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listMaterial = function($con) {
		$sql = 'SELECT * FROM material';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$materials = array();
			while ($result = mysqli_fetch_array($results)) {
				$material = new Material();
				$material->setDonationId($dbio->select($readDonation(result[0])));
				$material->setValue(result[1]);
				$material->setDescription(result[2]);
				$materials[] = material;
			}// end while
		} else {
			$materials = false;
		}
		return $materials;
	};

